import { SCREEN_WIDTH, SCREEN_HEIGHT } from '../render';

/**
 * 渲染管理器
 * 负责统一管理所有游戏对象的渲染，包括角色和特效
 */
export default class RenderManager {
  constructor(databus) {
    this.databus = databus;
    
    // 渲染层级定义
    this.layers = {
      BACKGROUND: 0,  // 背景层
      ALLY: 1,        // 队友层
      ENEMY: 2,       // 敌人层
      PLAYER: 3,      // 玩家层
      EFFECT: 4,      // 特效层
      UI: 5           // UI层
    };
    
    // 存储需要渲染的对象
    this.renderObjects = {
      background: null,
      allies: [],
      enemies: [],
      player: null,
      effects: [],
      ui: []
    };
  }
  
  /**
   * 设置背景对象
   * @param {Object} background - 背景对象
   */
  setBackground(background) {
    this.renderObjects.background = background;
  }
  
  /**
   * 设置玩家对象
   * @param {Object} player - 玩家对象
   */
  setPlayer(player) {
    this.renderObjects.player = player;
  }
  
  /**
   * 设置队友对象数组
   * @param {Array} allies - 队友对象数组
   */
  setAllies(allies) {
    this.renderObjects.allies = allies;
  }
  
  /**
   * 设置敌人对象数组
   * @param {Array} enemies - 敌人对象数组
   */
  setEnemies(enemies) {
    this.renderObjects.enemies = enemies;
  }
  
  /**
   * 添加UI元素
   * @param {Object} uiElement - UI元素对象
   */
  addUIElement(uiElement) {
    this.renderObjects.ui.push(uiElement);
  }
  
  /**
   * 清除UI元素
   */
  clearUIElements() {
    this.renderObjects.ui = [];
  }
  
  /**
   * 添加特效
   * @param {Object} effect - 特效对象
   */
  addEffect(effect) {
    this.renderObjects.effects.push(effect);
  }
  
  /**
   * 更新特效状态
   */
  updateEffects() {
    // 移除已完成的特效
    this.renderObjects.effects = this.renderObjects.effects.filter(effect => !effect.isFinished);
    
    // 更新剩余特效
    this.renderObjects.effects.forEach(effect => {
      if (typeof effect.update === 'function') {
        effect.update();
      }
    });
  }
  
  /**
   * 渲染波次信息
   * @param {CanvasRenderingContext2D} ctx - Canvas上下文
   * @param {Number} currentWave - 当前波次
   * @param {Boolean} waveCleared - 波次是否已清除
   * @param {Number} waveTimer - 波次计时器
   */
  renderWaveInfo(ctx, currentWave, waveCleared, waveTimer) {
    if (waveCleared && waveTimer > 0) {
      ctx.save();
      ctx.font = '30px Arial';
      ctx.fillStyle = '#FFFFFF';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.fillText(
        `Wave ${currentWave} Completed!`,
        SCREEN_WIDTH / 2,
        SCREEN_HEIGHT / 2 - 20
      );
      ctx.font = '20px Arial';
      ctx.fillText(
        `Next wave in ${Math.ceil(waveTimer / 60)}...`,
        SCREEN_WIDTH / 2,
        SCREEN_HEIGHT / 2 + 20
      );
      ctx.restore();
    }
  }
  
  /**
   * 渲染所有对象
   * @param {CanvasRenderingContext2D} ctx - Canvas上下文
   * @param {Object} waveInfo - 波次信息
   */
  render(ctx, waveInfo = null) {
    // 1. 渲染背景
    if (this.renderObjects.background && typeof this.renderObjects.background.render === 'function') {
      this.renderObjects.background.render(ctx);
    }
    
    // 2. 渲染队友
    this.renderObjects.allies.forEach(ally => {
      if (ally.visible && typeof ally.render === 'function') {

        
        ally.render(ctx);
      }
    });
    
    // 3. 渲染敌人
    this.renderObjects.enemies.forEach(enemy => {
      if (enemy.visible && typeof enemy.render === 'function') {
        enemy.render(ctx);
      }
    });
    
    // 4. 渲染玩家
    if (this.renderObjects.player && typeof this.renderObjects.player.render === 'function') {
      this.renderObjects.player.render(ctx);
    }
    
    // 5. 渲染特效
    this.renderObjects.effects.forEach(effect => {
      if (typeof effect.render === 'function') {
        effect.render(ctx);
      }
    });
    
    // 6. 渲染UI
    this.renderObjects.ui.forEach(ui => {
      if (typeof ui.render === 'function') {
        ui.render(ctx);
      }
    });
    
    // 7. 渲染波次信息（如果提供）
    if (waveInfo) {
      this.renderWaveInfo(
        ctx, 
        waveInfo.currentWave, 
        waveInfo.waveCleared, 
        waveInfo.waveTimer
      );
    }
  }
}